package com.oa.system.controller;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.core.BaseController;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.PostAddDTO;
import com.oa.system.dto.PostUpdateDTO;
import com.oa.system.dto.UserAddDTO;
import com.oa.system.service.IPostService;
import com.oa.system.service.IUserService;
import com.oa.system.vo.PostListVO;
import com.oa.system.vo.UserUpdateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/system")
@Api(description = "系统管理")
public class SystemController extends BaseController {

    private final IPostService postService;
    private final IUserService userService;


    public SystemController(IPostService postService, IUserService userService) {
        this.postService = postService;
        this.userService = userService;
    }


    /**
     * 岗位管理-新增岗位
     *
     * @param
     * @return Resp
     */
    @PostMapping("/addPost")
    @CommonController
    @Description("岗位管理-新增岗位")
    @ApiOperation(value = "岗位管理-新增岗位")
    public Resp addPost(@ApiParam(value = "岗位表对象") @RequestBody(required = false) PostAddDTO postAddDTO) throws BusinessException {
        return postService.addPost(postAddDTO);
    }

    /**
     * 岗位管理-根据条件查询岗位列表
     *
     * @param page  页码
     * @param limit 每页多少条
     * @return Resp
     */
    @PostMapping("/findPostListByCondition")
    @CommonController
    @Description("岗位管理-根据条件查询岗位列表")
    @ApiOperation(value = "岗位管理-根据条件查询岗位列表,不传条件则查询所有")
    public Resp<List<PostListVO>> findPostListByCondition(
            @ApiParam(value = "页码") @RequestParam(required = false) Integer page,
            @ApiParam(value = "查询条数") @RequestParam(required = false) Integer limit,
            @ApiParam(value = "岗位名称") @RequestParam(required = false) String postName) throws DataIsEmptyException {

        return new Resp().success(postService.findPostListByCondition(page, limit, postName));
    }

    /**
     * 岗位管理-根据岗位主键id查询岗位详情
     *
     * @param id id
     * @return Resp
     */
    @PostMapping("/findPostDetailsById")
    @CommonController
    @Description("岗位管理-根据岗位主键id查询岗位详情")
    @ApiOperation(value = "岗位管理-根据岗位主键id查询岗位详情")
    public Resp findPostDetailsById(@ApiParam(value = "id", required = true) @RequestParam String id) throws DataIsEmptyException {
        return postService.findPostDetailsById(id);
    }


    @PostMapping("/updatePostDetailsById")
    @CommonController
    @Description("岗位管理-根据岗位主键id编辑岗位")
    @ApiOperation(value = "岗位管理-根据岗位主键id编辑岗位")
    public Resp updatePostDetailsById(@ApiParam(value = "岗位表对象") @RequestBody(required = false) PostUpdateDTO postUpdateDTO) throws BusinessException {
        return postService.updatePostDetailsById(postUpdateDTO);
    }

    @PostMapping("/deletePostDetailsById")
    @CommonController
    @Description("岗位管理-根据岗位主键id删除岗位")
    @ApiOperation(value = "岗位管理-根据岗位主键id删除岗位")
    public Resp deletePostDetailsById(@ApiParam(value = "id", required = true) @RequestParam String id) throws DataIsEmptyException, BusinessException {
        return postService.deletePostDetailsById(id);
    }

    @PostMapping("/addUser")
    @CommonController
    @Description("用户管理-添加用户")
    @ApiOperation(value = "用户管理-添加用户")
    public Resp addUser(@ApiParam(value = "用户表对象") @RequestBody(required = false) UserAddDTO userAddDTO) throws BusinessException {
        return userService.addUser(userAddDTO);

    }

    @PostMapping("/findUserListByCondition")
    @CommonController
    @Description("用户管理-根据条件查询用户列表")
    @ApiOperation(value = "用户管理-根据条件查询用户列表")
    public Resp findUserListByCondition(@ApiParam(value = "页码") @RequestParam(required = false) Integer page,
                                        @ApiParam(value = "查询条数") @RequestParam(required = false) Integer limit,
                                        @ApiParam(value = "用户名") @RequestParam(required = false) String userName,
                                        @ApiParam(value = "姓名") @RequestParam(required = false) String name,
                                        @ApiParam(value = "创建时间") @RequestParam(required = false) String createTime,
                                        @ApiParam(value = "部门名称") @RequestParam(required = false) String organizationName) throws DataIsEmptyException {

        return new Resp().success(userService.findUserListByCondition(page, limit, userName, name, createTime, organizationName));
    }

    @PostMapping("/findUserDetailsById")
    @CommonController
    @Description("用户管理-根据id查询用户详情")
    @ApiOperation(value = "用户管理-根据id查询用户详情")
    public Resp findUserDetailsById(@ApiParam(value = "id", required = true) @RequestParam String id) throws DataIsEmptyException {
        return userService.findUserDetailsById(id);
    }

    @PostMapping("/updateUserDetailsById")
    @CommonController
    @Description("用户管理-根据id修改用户")
    @ApiOperation(value = "用户管理-根据id修改用户")
    public Resp updateUserDetailsById(@ApiParam(value = "用户表对象") @RequestBody(required = false) UserUpdateVO userUpdateVO) throws BusinessException {
        return userService.updateUserDetailsById(userUpdateVO);
    }


    @PostMapping("/EnableOrDisableUserById")
    @CommonController
    @Description("用户管理-根据ids启用或者禁用用户")
    @ApiOperation(value = "用户管理-根据ids启用或者禁用用户")
    public Resp EnableOrDisableUserById(@ApiParam(value = "如果是多个id使用,拼接起来", required = true) @RequestParam String ids,
                                        @ApiParam(value = "类型[1-启用 2-禁用]", required = true) @RequestParam String type) throws BusinessException {
        return userService.EnableOrDisableUserById(ids, type);
    }

    @PostMapping("/deleteUserById")
    @CommonController
    @Description("用户管理-根据id删除用户")
    @ApiOperation(value = "用户管理-根据id删除用户")
    public Resp deleteUserById(@ApiParam(value = "id", required = true) @RequestParam String id) throws BusinessException, DataIsEmptyException {
        return userService.deleteUserById(id);
    }


}
